
module.exports = function () {
    let t = this
    this.templace = {
        config_t_xml : (
            <relative id="temp_body" w="*" h="{{device.height}}px"
                padding="0"
                >
                <vertical w="*" h="*" id="temp_main"
                    layout_below="temp_body" 
                    w="*"
                    h="{{device.height * 0.8}}px"
                    layout_alignParentBottom="true"
                    >
                    <horizontal w="*" h="auto" padding="8dp">
                        <button id="cancel" layout_weight="1" textColor="#ffffff" style="Widget.AppCompat.Button.Borderless" text="取 消"></button>
                        <text id="title" text="--脚本设置--" textColor="#ffffff" textSize="18sp" margin="12dp 0 12dp 0" textStyle="bold" gravity="center" layout_weight="10"></text>
                        <button id="save" layout_weight="1" textColor="#ffffff" style="Widget.AppCompat.Button.Borderless" text="保 存"></button>
                    </horizontal>
                    <vertical w="*" h="*" id="user_body" marginTop="6dp" bg="#ffffff" padding="2dp 0 2dp 2dp">

                    </vertical>
                </vertical>
            </relative>
        ),
        config_t_cli : function () {
            console.log("配置载入成功")
            t.store.__obj__.config_window.cancel.click(() => {
                console.log("取消")
                t.dh.close_pupwindow(t.store.__obj__.config_window.temp_main, function () {
                    t.store.__obj__.config_window.setSize(0, 0)
                    t.mutations.close_orerlay()
                    t.store.__status__.setting_status = !t.store.__status__.setting_status
                })
            })
            // ----
            t.store.__obj__.config_window.save.click(() => {
                console.log("保存")
                //开始保存数据
                t.dh.close_pupwindow(t.store.__obj__.config_window.temp_main, () => {
                    t.mutations.save_view()
                    
                    t.store.__obj__.config_window.setSize(0, 0)
                    t.mutations.close_orerlay()
                    t.store.__status__.setting_status = !t.store.__status__.setting_status
                })
            })
        }
}

    this.usercs = {
        config_t_xml : (
            <relative id="temp_body" w="*" h="{{device.height}}px"
                padding="0"
                >
                <vertical w="*" h="*" id="temp_main"
                    layout_below="temp_body" 
                    w="*"
                    h="{{device.height * 0.8}}px"
                    layout_alignParentBottom="true"
                    >
                    <horizontal w="*" h="auto" padding="8dp">
                        <button id="cancel" layout_weight="1" textColor="#ffffff" text="取 消" style="Widget.AppCompat.Button.Borderless"></button>
                        
                        <text id="title" text="--用户中心--" textColor="#ffffff" textSize="18sp" margin="12dp 0 12dp 0" textStyle="bold" gravity="center" layout_weight="10"></text>
                        <button visibility="invisible" textColor="#ffffff" layout_weight="1" text="登 录"></button>
                    </horizontal>
                    <vertical w="*" h="*" id="user_body">
                        <relative w="*" h="*" id="user_user" visibility="visible" bg="#FFF5F6FA">
                            <vertical id="head" w='*'>
                                {/* <text text="请登录！"></text> */}
                                
                                <horizontal>
                                    <button id="login" layout_weight="1" text="登 录"></button>
                                    <button id="rechar" layout_weight="1" text="充 值"></button>
                                    <button id="unbound" layout_weight="1" text="解 绑"></button>
                                    <button id="trial" layout_weight="1" text="试 用"></button>
                                </horizontal>
                            </vertical>
                            
                            <vertical id="body" layout_below="head" w="*">
                                <list id="user_list_view" visibility="gone" w="*" marginTop="5dp">
                                    <vertical w='*' visibility="{{visibility}}" >
                                        <relative w='*' bg="#ffffff" paddingTop="4dp" paddingBottom="4dp">
                                            <img margin="2dp" src="{{icon}}" layout_centerVertical="true" id="icon" w="25dp" h="25dp" ></img>
                                            <text marginLeft="6dp" text="{{title}}" layout_toRightOf="icon" layout_centerVertical="true" id="title"></text>
                                            <text text="{{value}}" layout_toRightOf="title" layout_centerVertical="true" id="value"></text>
                                        </relative>
                                    </vertical>

                                </list>

                                <list id="device_list_view" w="*" marginTop="5dp">
                                    <vertical w='*' visibility="{{visibility}}" >
                                        <relative w='*' bg="#ffffff" paddingTop="4dp" paddingBottom="4dp">
                                            <img margin="2dp" src="{{icon}}" layout_centerVertical="true" id="icon" w="25dp" h="25dp" ></img>
                                            <text marginLeft="6dp" text="{{title}}" layout_toRightOf="icon" layout_centerVertical="true" id="title"></text>
                                            <text text="{{value}}" layout_toRightOf="title" layout_centerVertical="true" id="value"></text>
                                        </relative>
                                    </vertical>
                                </list>
                            </vertical>
                        </relative>
                        
                        {/* 登录界面 */}
                        <vertical id="user_login" w="*" padding="8dp" visibility="gone">

                            <card w="*" h="auto" contentPadding="5dp"
                                cardCornerRadius="10dp"
                                cardElevation="2dp"
                            >
                                <vertical>
                                    <text text="欢迎使用AutoJs 班花模板！！"></text>
                                    <horizontal>
                                        <button style="Widget.AppCompat.Button.Colored" text="登 录" layout_weight="1"></button>
                                        <button text="试 用" layout_weight="1"></button>
                                    </horizontal>
                                </vertical>
                            </card>
                        </vertical>
                    </vertical>
                </vertical>
            </relative>
        ),
        config_t_cli : function () {
            console.log("我的载入成功")
            //处理列表数据
            t.store.__user_list__.map((v, i) => {
                return v.map((y, j) => {
                    y.icon = t.mutations.getImageBase(y.icon)
                    return y
                })
            })
            
            t.store.__user_list__[1].map((v, i) => {
                switch (v.title) {
                    case "安卓版本：":
                        v.value = device.release
                    break;
                    case "设备型号：":
                        v.value = ""+device.product+" "+device.model
                    break;
                    case "IMEI：":
                        v.value = device.getIMEI()
                    break;
                }
                return v
            })

            // 载入列表
            ui.run(() => {
                let kami = t.storage.get("pjysdk_kami")
                if (kami != null && kami != "") {
                    //自动登录
                    t.pjysdk.SetCard(kami)
                    t.mutations.pjysdk_login()
                    // t.store.__obj__.user_window.user_list_view.setDataSource(t.store.__user_list__[0]);
                }
                t.store.__obj__.user_window.device_list_view.setDataSource(t.store.__user_list__[1]);
            })
            
            // 点击取消
            t.store.__obj__.user_window.cancel.click(() => {
                console.log("取消")
                t.dh.close_pupwindow(t.store.__obj__.user_window.temp_main, () => {
                    t.store.__obj__.user_window.setSize(0, 0)
                    t.mutations.close_orerlay()
                    t.store.__status__.user_status = !t.store.__status__.user_status
                })
            })
            
            //登录
            t.store.__obj__.user_window.login.click(() => {
                console.log("登录")
                let cli = getClip()
                if (cli.length != 20) {
                    cli = ""
                }
                dialogs.rawInput("请输入卡密：", cli).then(v => {
                    console.log(v)
                    t.pjysdk.SetCard(v)
                    t.mutations.pjysdk_login()
                })
            })

            //充值
            t.store.__obj__.user_window.rechar.click(() => {
                console.log("充值")
                let cli = getClip()
                if (cli.length != 20) {
                    cli = ""
                }
                dialogs.rawInput("请输入充值卡：", cli).then(v => {
                    console.log(v)
                    let result = t.pjysdk.CardRecharge(t.pjysdk._card, v)
                    if (result.code == 0) {
                        console.log("充值成功")
                        toastLog("充值成功")
                        //重新调用登录方法
                        t.mutations.pjysdk_login()
                        
                    } else {
                        console.log("充值失败")
                        toastLog(result.message)

                    }
                    
                })
            })

            //解绑
            t.store.__obj__.user_window.unbound.click(() => {
                dialogs.confirm("确定要解绑吗？").then(clear => {
                    if (clear) {
                        let ret = t.pjysdk.CardUnbindDevice()
                        if (ret.code == 0) {
                            console.log("解绑成功")
                            t.storage.remove("pjysdk_kami")
                            t.storage.remove("pjysdk_login_ret")
                            t.store.__obj__.user_window.user_list_view.setVisibility(android.view.View.GONE)
                        } else {
                            console.log("解绑失败")
                            console.log(ret.message)
                        }
                    }
                })
            })

            //试用
            t.store.__obj__.user_window.trial.click(() => {
                console.log("试用登录")
                if (t.pjysdk._card != "" && t.pjysdk._card != null) {
                    dialogs.confirm("确定要试用？").then(clear => {
                        if (clear) {
                            
                            let ret = t.pjysdk.TrialLogin()
                    
                            if (ret.code == 0) {
                                console.log(ret)
                                ret.result.card_type = "试用"
                                t.mutations.pjysdk_updata(ret.result)
                                t.storage.put("pjysdk_login_ret", ret.result)
            
                                
                            } else {
                                console.log("试用登录失败")
                                console.log(ret.message)

                            }
                        }
                    })
                    
                }
            })
            
            // ----
            // t.store.__obj__.user_window.login.click(() => {
            //     console.log("登录")
            //     t.dh.close_pupwindow(t.store.__obj__.user_window.temp_main, () => {
            //         t.store.__obj__.user_window.setSize(0, 0)
            //         t.mutations.close_orerlay()
            //         t.store.__status__.user_status = !t.store.__status__.user_status
            //     })
            // })
        }
    }
}

